export const BUTTONS = {
	gp2040: {
		label: 'GP2040',
		value: 'gp2040',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: 'B1',
		B2: 'B2',
		B3: 'B3',
		B4: 'B4',
		L1: 'L1',
		R1: 'R1',
		L2: 'L2',
		R2: 'R2',
		S1: 'S1',
		S2: 'S2',
		L3: 'L3',
		R3: 'R3',
		A1: 'A1',
		A2: 'A2',
		A3: 'A3',
		A4: 'A4',
		E1: 'Extra 1',
		E2: 'Extra 2',
		E3: 'Extra 3',
		E4: 'Extra 4',
		E5: 'Extra 5',
		E6: 'Extra 6',
		E7: 'Extra 7',
		E8: 'Extra 8',
		E9: 'Extra 9',
		E10: 'Extra 10',
		E11: 'Extra 11',
		E12: 'Extra 12',
		Fn: 'Function',
	},
	arcade: {
		label: 'Arcade',
		value: 'arcade',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: 'K1',
		B2: 'K2',
		B3: 'P1',
		B4: 'P2',
		L1: 'P4',
		R1: 'P3',
		L2: 'K4',
		R2: 'K3',
		S1: 'Select',
		S2: 'Start',
		L3: 'L3',
		R3: 'R3',
		A1: 'Home',
		A2: '-',
		A3: '-',
		A4: '-',
		E1: 'Extra 1',
		E2: 'Extra 2',
		E3: 'Extra 3',
		E4: 'Extra 4',
		E5: 'Extra 5',
		E6: 'Extra 6',
		E7: 'Extra 7',
		E8: 'Extra 8',
		E9: 'Extra 9',
		E10: 'Extra 10',
		E11: 'Extra 11',
		E12: 'Extra 12',
		Fn: 'Function',
	},
	xinput: {
		label: 'XInput',
		value: 'xinput',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: 'A',
		B2: 'B',
		B3: 'X',
		B4: 'Y',
		L1: 'LB',
		R1: 'RB',
		L2: 'LT',
		R2: 'RT',
		S1: 'Back',
		S2: 'Start',
		L3: 'LS',
		R3: 'RS',
		A1: 'Guide',
		A2: '-',
		A3: '-',
		A4: '-',
		E1: '-',
		E2: '-',
		E3: '-',
		E4: '-',
		E5: '-',
		E6: '-',
		E7: '-',
		E8: '-',
		E9: '-',
		E10: '-',
		E11: '-',
		E12: '-',
		Fn: 'Function',
	},
	switch: {
		label: 'Switch',
		value: 'switch',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: 'B',
		B2: 'A',
		B3: 'Y',
		B4: 'X',
		L1: 'L',
		R1: 'R',
		L2: 'ZL',
		R2: 'ZR',
		S1: 'Minus',
		S2: 'Plus',
		L3: 'LS',
		R3: 'RS',
		A1: 'Home',
		A2: 'Capture',
		A3: '-',
		A4: '-',
		E1: '-',
		E2: '-',
		E3: '-',
		E4: '-',
		E5: '-',
		E6: '-',
		E7: '-',
		E8: '-',
		E9: '-',
		E10: '-',
		E11: '-',
		E12: '-',
		Fn: 'Function',
	},
	ps3: {
		label: 'PS3',
		value: 'ps3',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: 'Cross',
		B2: 'Circle',
		B3: 'Square',
		B4: 'Triangle',
		L1: 'L1',
		R1: 'R1',
		L2: 'L2',
		R2: 'R2',
		S1: 'Select',
		S2: 'Start',
		L3: 'L3',
		R3: 'R3',
		A1: 'PS',
		A2: '-',
		A3: '-',
		A4: '-',
		E1: '-',
		E2: '-',
		E3: '-',
		E4: '-',
		E5: '-',
		E6: '-',
		E7: '-',
		E8: '-',
		E9: '-',
		E10: '-',
		E11: '-',
		E12: '-',
		Fn: 'Function',
	},
	ps4: {
		label: 'PS4',
		value: 'ps4',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: 'Cross',
		B2: 'Circle',
		B3: 'Square',
		B4: 'Triangle',
		L1: 'L1',
		R1: 'R1',
		L2: 'L2',
		R2: 'R2',
		S1: 'Share',
		S2: 'Options',
		L3: 'L3',
		R3: 'R3',
		A1: 'PS',
		A2: 'Touchpad Center',
		A3: 'Touchpad Left',
		A4: 'Touchpad Right',
		A2: 'Touchpad',
		E1: '-',
		E2: '-',
		E3: '-',
		E4: '-',
		E5: '-',
		E6: '-',
		E7: '-',
		E8: '-',
		E9: '-',
		E10: '-',
		E11: '-',
		E12: '-',
		Fn: 'Function',
	},
	dinput: {
		label: 'Generic',
		value: 'dinput',
		Up: 'Up',
		Down: 'Down',
		Left: 'Left',
		Right: 'Right',
		B1: '2',
		B2: '3',
		B3: '1',
		B4: '4',
		L1: '5',
		R1: '6',
		L2: '7',
		R2: '8',
		S1: '9',
		S2: '10',
		L3: '11',
		R3: '12',
		A1: '13',
		A2: '14',
		A3: '15',
		A4: '16',
		E1: '21',
		E2: '22',
		E3: '23',
		E4: '24',
		E5: '25',
		E6: '26',
		E7: '27',
		E8: '28',
		E9: '29',
		E10: '30',
		E11: '31',
		E12: '32',
		Fn: 'Function',
	},
};

const AUX_BUTTONS = ['S1', 'S2', 'L3', 'R3', 'A1', 'A2'];
const AUX_BUTTONS_STICKLESS_13 = ['S1', 'S2', 'L3', 'R3', 'A2'];
const AUX_BUTTONS_STICKLESS_14 = ['S1', 'S2', 'A1', 'A2'];
const AUX_BUTTONS_STICKLESS_16 = ['S1', 'S2'];
const AUX_BUTTONS_STICKLESS_16_A = ['S1', 'S2'];
const MAIN_BUTTONS = [
	'Up',
	'Down',
	'Left',
	'Right',
	'B1',
	'B2',
	'B3',
	'B4',
	'L1',
	'R1',
	'L2',
	'R2',
];
const MAIN_BUTTONS_STICKLESS_13 = [
	'Up',
	'A1',
	'Down',
	'Left',
	'Right',
	'B1',
	'B2',
	'B3',
	'B4',
	'L1',
	'R1',
	'L2',
	'R2',
];
const MAIN_BUTTONS_STICKLESS_14 = [
	'Up',
	'Down',
	'Left',
	'Right',
	'B1',
	'B2',
	'B3',
	'B4',
	'L1',
	'R1',
	'L2',
	'R2',
	'L3',
	'R3',
];
const MAIN_BUTTONS_STICKLESS_16 = [
	'Up',
	'A1',
	'Down',
	'Left',
	'Right',
	'B1',
	'B2',
	'B3',
	'B4',
	'L1',
	'R1',
	'L2',
	'R2',
	'R3',
	'L3',
	'A2',
];
const MAIN_BUTTONS_STICKLESS_16_A = [
	'Up',
	'A1',
	'Down',
	'Left',
	'Right',
	'B1',
	'B2',
	'B3',
	'B4',
	'L1',
	'R1',
	'L2',
	'R2',
	'R3',
	'L3',
	'A2',
];

const STICK_LAYOUT = [
	[null, 'Left', null],
	['Up', null, 'Down'],
	[null, 'Right', null],
	['B3', null, 'B1'],
	['B4', null, 'B2'],
	['R1', null, 'R2'],
	['L1', null, 'L2'],
];

const STICKLESS_LAYOUT = [
	['Left', null, null],
	['Down', null, null],
	['Right', null, null],
	[null, 'Up', null],
	['B3', 'B1', null],
	['B4', 'B2', null],
	['R1', 'R2', null],
	['L1', 'L2', null],
];

const STICKLESS_13_LAYOUT = [
	['Left', null, null],
	['Down', null, null],
	['A1', null, null],
	['Right', null, null],
	[null, 'Up', null],
	['B3', 'B1', null],
	['B4', 'B2', null],
	['R1', 'R2', null],
	['L1', 'L2', null],
];

const STICKLESS_14_LAYOUT = [
	['L3', null, null],
	['Left', null, null],
	['Down', null, null],
	['Right', null, null],
	['B3', 'B1', 'Up'],
	['B4', 'B2', 'R3'],
	['R1', 'R2', null],
	['L1', 'L2', null],
];

const STICKLESS_16_LAYOUT = [
	[null, 'Left', null, null],
	['A1', 'Down', null, null],
	[null, 'Right', null, 'L3'],
	['A2', 'B3', 'B1', 'Up'],
	[null, 'B4', 'B2', 'R3'],
	[null, 'R1', 'R2', null],
	[null, 'L1', 'L2', null],
];

const STICKLESS_16_A_LAYOUT = [
	['L3', null, null, null],
	[null, 'Left', null, null],
	['A1', 'Down', null, null],
	[null, 'Right', null, null],
	['A2', 'B3', 'B1', 'Up'],
	[null, 'B4', 'B2', 'R3'],
	[null, 'R1', 'R2', null],
	[null, 'L1', 'L2', null],
];

const KEYBOARD_LAYOUT = [
	[null, 'Left'],
	['Up', 'Down'],
	[null, 'Right'],
	['B3', 'B1'],
	['B4', 'B2'],
	['R1', 'R2'],
	['L1', 'L2'],
];
export const DPAD_MASKS = [
	{ label: 'Up', value: 1 << 0 },
	{ label: 'Down', value: 1 << 1 },
	{ label: 'Left', value: 1 << 2 },
	{ label: 'Right', value: 1 << 3 },
];

export const BUTTON_LAYOUTS = [
	{
		label: 'Stick',
		value: 0,
		stickLayout: 'standard',
		matrix: STICK_LAYOUT,
		auxButtons: AUX_BUTTONS,
		mainButtons: MAIN_BUTTONS,
	},
	{
		label: 'Stickless',
		value: 1,
		stickLayout: 'stickless',
		matrix: STICKLESS_LAYOUT,
		auxButtons: AUX_BUTTONS,
		mainButtons: MAIN_BUTTONS,
	},
	{
		label: 'Stickless 13',
		value: 2,
		stickLayout: 'stickless-13',
		matrix: STICKLESS_13_LAYOUT,
		auxButtons: AUX_BUTTONS_STICKLESS_13,
		mainButtons: MAIN_BUTTONS_STICKLESS_13,
	},
	{
		label: 'Stickless 14',
		value: 3,
		stickLayout: 'stickless-14',
		matrix: STICKLESS_14_LAYOUT,
		auxButtons: AUX_BUTTONS_STICKLESS_14,
		mainButtons: MAIN_BUTTONS_STICKLESS_14,
	},
	{
		label: 'Stickless 16',
		value: 4,
		stickLayout: 'stickless-16',
		matrix: STICKLESS_16_LAYOUT,
		auxButtons: AUX_BUTTONS_STICKLESS_16,
		mainButtons: MAIN_BUTTONS_STICKLESS_16,
	},
	{
		label: 'Stickless 16 A',
		value: 5,
		stickLayout: 'stickless-16-a',
		matrix: STICKLESS_16_A_LAYOUT,
		auxButtons: AUX_BUTTONS_STICKLESS_16_A,
		mainButtons: MAIN_BUTTONS_STICKLESS_16_A,
	},
	{
		label: 'WASD',
		value: 6,
		stickLayout: 'keyboard',
		matrix: KEYBOARD_LAYOUT,
		auxButtons: AUX_BUTTONS,
		mainButtons: MAIN_BUTTONS,
	},
];

export const BUTTON_MASKS = [
	{ label: 'B1', value: 1 << 0 },
	{ label: 'B2', value: 1 << 1 },
	{ label: 'B3', value: 1 << 2 },
	{ label: 'B4', value: 1 << 3 },
	{ label: 'L1', value: 1 << 4 },
	{ label: 'R1', value: 1 << 5 },
	{ label: 'L2', value: 1 << 6 },
	{ label: 'R2', value: 1 << 7 },
	{ label: 'S1', value: 1 << 8 },
	{ label: 'S2', value: 1 << 9 },
	{ label: 'L3', value: 1 << 10 },
	{ label: 'R3', value: 1 << 11 },
	{ label: 'A1', value: 1 << 12 },
	{ label: 'A2', value: 1 << 13 },
	{ label: 'A3', value: 1 << 14 },
	{ label: 'A4', value: 1 << 15 },
];

export const BUTTON_MASKS_OPTIONS = [
	{ label: 'None', value: 0 },
	...BUTTON_MASKS,
	{ label: 'Up', value: 1 << 16 },
	{ label: 'Down', value: 1 << 17 },
	{ label: 'Left', value: 1 << 18 },
	{ label: 'Right', value: 1 << 19 },
	{ label: 'E1', value: 1 << 20 },
	{ label: 'E2', value: 1 << 21 },
	{ label: 'E3', value: 1 << 22 },
	{ label: 'E4', value: 1 << 23 },
	{ label: 'E5', value: 1 << 24 },
	{ label: 'E6', value: 1 << 25 },
	{ label: 'E7', value: 1 << 26 },
	{ label: 'E8', value: 1 << 27 },
	{ label: 'E9', value: 1 << 28 },
	{ label: 'E10', value: 1 << 29 },
	{ label: 'E11', value: 1 << 30 },
	{ label: 'E12', value: 1 << 31 },
];

export const ANALOG_PINS = [26, 27, 28, 29];

// deep copy and swp
export const getButtonLabels = (labelType, swapTpShareLabels = false) => {
	const buttons = BUTTONS[labelType];

	if (labelType == 'ps4' && swapTpShareLabels) {
		const buttonLabelS1 = buttons['S1'];
		const buttonLabelA2 = buttons['A2'];
		return {
			...buttons,
			S1: buttonLabelA2,
			A2: buttonLabelS1,
		};
	} else {
		return buttons;
	}
};
